package de.unimannheim.ki.servlets;

import java.io.InputStream;
import java.net.URI;
import java.net.URLEncoder;



import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnManagerPNames;
import org.apache.http.conn.params.ConnPerRouteBean;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.commons.codec.*;
import org.apache.commons.codec.net.URLCodec;

public class WebServiceClient {

	private HttpClient httpclient;
	
	private WebServiceClient wsClient;
	public WebServiceClient getWebServiceClient() {
		if(this.wsClient== null){
			wsClient= new WebServiceClient();
			return wsClient;
		}
		return wsClient;
	}
	
	public WebServiceClient() {
		super();
		this.httpclient = createHttpClient();
	}

	@SuppressWarnings("deprecation")
    private static HttpClient createHttpClient() {
        HttpClient httpClient;
		
		SchemeRegistry schemeRegistry = new SchemeRegistry();
		schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
		schemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(), 443));
		 
		HttpParams params = new BasicHttpParams();
		params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30);
		params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(30));
		params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
		HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
		 
		ThreadSafeClientConnManager connMgr = new ThreadSafeClientConnManager(new BasicHttpParams(),schemeRegistry);
		httpClient = new DefaultHttpClient(connMgr, params);
		return httpClient;
    }
	
	public InputStream perfomSynchCall(String url) throws Exception{
	
		HttpGet getMethod = new HttpGet(url);

		System.out.println(getMethod.getRequestLine());
		
		HttpResponse httpResponse = null;
		try
		{	
			httpResponse = httpclient.execute(getMethod);
			
		}
		catch(Exception e)
		{
			e.printStackTrace();
			throw new Exception();
			
		}
		return httpResponse.getEntity().getContent();
	}
	
	public InputStream performQuery(String query, String access_token)throws Exception{
		URLCodec encoder = new URLCodec();
	
		HttpGet getMethod = new HttpGet("https://graph.facebook.com/fql?q="+encoder.encode(query)+"&access_token="+encoder.encode(access_token));
		
		HttpResponse httpResponse = null;
		try
		{	
			httpResponse = httpclient.execute(getMethod);
			
		}
		catch(Exception e)
		{
			throw new Exception();
			
		}

		return httpResponse.getEntity().getContent();
	}
	

}
